package org.ydsw.data.manage.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.ydsw.data.manage.entity.User;
import org.ydsw.data.manage.service.impl.UserServiceimpl;
import org.ydsw.data.manage.utils.JsonResponse;


import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserServiceimpl userServiceimpl;

    @GetMapping("findAll")
    public List<User> findAll() {
        return userServiceimpl.findAll();
    }

    @PostMapping("/login/isPass")
    public JsonResponse isPass(@RequestBody User user, HttpSession session) {
        if (user.getUsername().length()< 1){
            return JsonResponse.responseFail(500,"用户名不能为空!");
        }
        if (user.getPassword().length()< 1){
            return JsonResponse.responseFail(500,"密码不能为空!");
        }
        int userCount = userServiceimpl.getUserCount(user.getUsername());
        if (userCount < 1){
            return JsonResponse.responseFail(500,"用户不存在");
        }
        String password = user.getPassword();
        String username = user.getUsername();
        boolean ispass = userServiceimpl.isPass(username, password);
        if (ispass){
            session.setAttribute("loginUser",username);
            return JsonResponse.respSuccess(0,"登录成功",null);
        }else {
            return JsonResponse.responseFail(500,"密码错误");
        }

    }





}


